package com.gonfre.quientelo;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.gc.materialdesign.widgets.Dialog;

import util.AsyncRequest;
import util.MyParameters;
import util.Response;


public class RegisterPurchaserActivity extends MyActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_purchaser);
    }

    public void buttonSaveOnClick(View w) {
        AsyncRequest req = new AsyncRequest(this);
        Response res = new Response();
        Dialog dialog;

        String pName = getViewValueStr(R.id.editTextName);
        String pPhone = getViewValueStr(R.id.editTextPhone);
        String pEmail = getViewValueStr(R.id.editTextEmail);
        String pPass1 = getViewValueStr(R.id.editTextPassword);
        String pPass2 = getViewValueStr(R.id.editTextConfirmPassword);

        boolean passValidations = true;

        MyParameters p = new util.MyParameters();

        p.add(pName, "Nombre");
        p.add(pEmail, "Email");
        p.add(pPass1, "Password");
        p.add(pPass2, "Confirmar Password");

        String mandatory = p.validateMandatoryData();
        if(mandatory.compareTo("") != 0){
            passValidations = false;
            dialog = new Dialog(this,null, mandatory);
            dialog.show();
            return;
        }

        if(!validatePass(pPass1, pPass2)){
            passValidations = false;
            dialog = new Dialog(this,null, "Verifique el password ingresado.");
            dialog.show();
        }

        if(!validateEmail(pEmail)){
            passValidations = false;
            dialog = new Dialog(this,null, "El Email ingresado no cumple con el formato de correo.");
            dialog.show();
        }

        if(passValidations){
            startProgressBarCircular(R.id.progressBarRegisterPurchaserLayout);
            res = req.xRegistrerPurchaser(pName, pPhone, pEmail, pPass1, 75);
        }

    }

    @Override
    public void onFinishRequest(int type, Response res) {
        stopProgressBarCircular(R.id.progressBarRegisterPurchaserLayout);
        Dialog dialog = new Dialog(this,null, res.getHeaderResponse().toString() + "-" + res.getHeaderMessage());
        dialog.show();
    }
}
